package com.shuiyun.chat.enums;

import com.shuiyun.chat.entity.message.*;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

/**
 * @author shuiyun
 * @program oicq
 * @description: 消息类型枚举
 * @create 2021-05-12 16:28
 **/
@Slf4j
public enum MessageType {
    // 消息类型
    TEST_MESSAGE(0, TestMessage.class),
    LOGIN_REQUEST_MESSAGE(1, LoginRequestMessage.class),
    LOGIN_RESPONSE_MESSAGE(2, LoginResponseMessage.class),
    SIMPLE_CHAT_MESSAGE(3, SimpleChatMessage.class),
    SEND_GROUP_MESSAGE(4,SendGroupMessage.class),
    RECEIVE_GROUP_MESSAGE(5,ReceiveGroupMessage.class);


    @Getter
    private final Integer type;
    @Getter
    private final Class clazz;

    <T> MessageType(Integer type, Class<T> clazz){
        this.type = type;
        this.clazz = clazz;
    }

    public static Message getMessage(Integer type){
        for (MessageType messageType : values()){
            if (messageType.type.equals(type)){
                try {
                    return (Message) messageType.getClazz().newInstance();
                }catch (Exception e){
                    log.error("根据type查询消息类型失败：{}",type);
                    return null;
                }
            }
        }
        log.error("根据type查询消息类型查询为空：{}",type);
        return null;
    }
}
